package demo.model;

import java.util.Comparator;

public class Contact {

	private String firstName;
	private String lastName;
	private String phoneNumber;

	private AddressBook addressBook;

	public Contact(AddressBook book) {
		assert book != null;
		addressBook = book;
	}

	public Contact(AddressBook book, String first, String last, String phone) {
		this(book);
		setInformations(first, last, phone);
	}

	public AddressBook getAddressBook() {
		return addressBook;
	}

	public void setInformations(String first, String last, String phone) {
		firstName = first;
		lastName = last;
		phoneNumber = phone;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String first) {
		firstName = first;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String last) {
		lastName = last;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phone) {
		phoneNumber = phone;
	}

	public String toString() {
		return "[ " + firstName + " " + lastName + " " + phoneNumber + "]";
	}
	
	public static Comparator<Contact> getNaturalComparator() {
		return new Comparator<Contact>() {
			@Override
			public int compare(Contact o1, Contact o2) {
				int res;
				if (o1 == o2)
					return 0;
				res = o1.lastName.compareToIgnoreCase(o2.lastName);
				if(res == 0)
					res = o1.firstName.compareToIgnoreCase(o2.firstName);
				if(res == 0)
					res = o1.phoneNumber.compareToIgnoreCase(o2.phoneNumber);
				if(res == 0)
					res = o1.hashCode() < o2.hashCode() ? -1 : 1;
				return res;
			}
		};
	}
}
